﻿using System;
using System.Linq;
using ATTools.Models;
using ATTools.Services;
using Orchard.Commands;
using Orchard.ContentManagement;
using Orchard.Core.Common.Models;
using Orchard.Core.Routable.Models;
using Orchard.Environment.Extensions;
using Orchard.Media.Services;
using Orchard.Settings;
using Orchard.Utility.Extensions;

namespace ATTools.Commands
{
    [OrchardFeature("ATTools")]
    public class SoundPartCommands : DefaultOrchardCommandHandler
    {
        private readonly IContentManager _contentManager;
        private readonly IMediaService _mediaService;
        private readonly ISiteService _siteService;
        private readonly ISpeakService _speakService;
        private readonly ITranslationService _translationService;

        public SoundPartCommands(IContentManager contentManager, IMediaService mediaService, ISiteService siteService, ISpeakService speakService, ITranslationService translationService)
        {
            _contentManager = contentManager;
            _mediaService = mediaService;
            _siteService = siteService;
            _speakService = speakService;
            _translationService = translationService;
        }

        [CommandName("soundpart update")]
        [CommandHelp("soundpart update\r\n\t" + "Updates all sound parts on your website")]
        public string Update()
        {
            var contentItemsWithSound = _contentManager.Query().Join<SoundRecord>().List();

            //Update each soundfile
            foreach (var item in contentItemsWithSound)
            {
                var bodyPart = item.As<BodyPart>();
                var soundPart = item.As<SoundPart>();
                var routePart = item.As<RoutePart>();
                
                if (soundPart.UseCustomSound)
                    continue;

                Context.Output.WriteLine(T("Updating sound: ") + routePart.Title + " (ID: " + item.Id.ToString() + ")");

                string mediaFolder = "ATTools-Soundfiles";
                string fileName = item.Id + ".wav";

                try
                {
                    _mediaService.DeleteFile(mediaFolder, fileName);
                }
                catch (Exception)
                {
                    ;
                }

                var speakSettings = _siteService.GetSiteSettings().As<ATToolsSettingsPart>().Record;

                string text = bodyPart.Text.RemoveTags();

                string language = soundPart.Record.LanguageOfSound;
                if (language == null || language == "Auto detect")
                {
                    string[] texts = new string[1]{text};
                    language = _translationService.DetectLanguages(texts).ElementAt(0);

                }

                string url = _speakService.SaveTextAsSoundFile(text, language, mediaFolder, fileName);
                soundPart.SoundString = url;
            }

            return "Sound Parts updated successfully";
        }
    }
}